<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="nickname" header-align="center" align="center" label="昵称"></el-table-column>
      <el-table-column prop="avatarUrl" header-align="center" align="center" label="头像">
        <template slot-scope="scope">
          <img 
            v-if="scope.row.avatarUrl" 
            :src="scope.row.avatarUrl" 
            style="width: 50px; height: 50px; border-radius: 50%; object-fit: cover;"
            @error="handleImageError($event)"
          >
          <span v-else>暂无头像</span>
        </template>
      </el-table-column>
      <el-table-column prop="gender" header-align="center" align="center" label="性别"></el-table-column>
      <el-table-column prop="birth" header-align="center" align="center" label="生日"></el-table-column>
      <el-table-column prop="personalProfile" header-align="center" align="center" label="个人简介"></el-table-column>
      <el-table-column prop="province" header-align="center" align="center" label="省"></el-table-column>
      <el-table-column prop="area" header-align="center" align="center" label="区"></el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="启用状态">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949" :active-value="1"
            :inactive-value="0" @change="handleStatusChange(scope.row.userId, scope.row.status)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="levelName" header-align="center" align="center" label="用户等级"></el-table-column>
      <el-table-column prop="currentPoints" header-align="center" align="center" label="积分"></el-table-column>
      <el-table-column prop="createType" header-align="center" align="center" label="创建类型 "></el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="创建时间"></el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dataForm: {
        key: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: []
    }
  },
  components: {},
  activated() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl("/admin/user/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          key: this.dataForm.key,
        }),
      }).then(({ data }) => {
        console.log(data);
        
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 处理用户状态变更
    handleStatusChange(userId, status) {
      this.$http({
        url: this.$http.adornUrl('/admin/user/updateStatus'),
        method: 'post',
        data: this.$http.adornData({
          userId: userId,
          status: status
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: '状态更新成功',
            type: 'success',
            duration: 1500
          })
        } else {
          this.$message.error(data.msg || '状态更新失败')
          // 如果更新失败，恢复原来的状态
          this.getDataList()
        }
      }).catch(() => {
        this.$message.error('网络错误，请重试')
        // 网络错误时，恢复原来的状态
        this.getDataList()
      })
    },
    // 处理图片加载错误
    handleImageError(event) {
      event.target.src = ''
      event.target.alt = '头像加载失败'
    }
  },
}
</script>
